<template>
  <PageWrapper contentFullHeight fixedHeight contentClass="main-box">
    <CommonDateSearch ref="searchRef" @search="handleSearch"></CommonDateSearch>
    <a-card :class="['common-card flex', typeList.length > 4 && 'list-more']" title="各系统日处理水量及成本趋势图">
      <div class="chart-box" v-for="item in typeList" :key="item.dictCode">
        <BarLine class="chart" v-if="item.type == 0" :loading="item.loading" :chartData="item.chartData"
          :title="item.itemName" :unit='item.chartData.yaxisList[0]?.unit || ""' />
        <Line class="chart" v-else :loading="item.loading" :chartData="item.chartData" :title="item.itemName" :unit='item.chartData.yaxisList[0]?.unit || ""' />
      </div>
    </a-card>
  </PageWrapper>
</template>
<script setup lang="ts">
import BarLine from '@/components/Charts/BarLine/index.vue'
import Line from '@/components/Charts/Line/index.vue'
import { ref, onMounted, reactive } from 'vue'
import { PageWrapper } from '@/components/Page'
// import CommonDateSearch from '@/components/CommonDateSearch/index.vue'
import CommonDateSearch from '../../CommonDateSearch.vue'
import { getSingleCaseUsageAndComprehensiveCost, getChildDataCostDiv } from '@/api/common/chart'
import { getDictListByCode } from '@/api/sys/dict'
import { CommonTypeList } from '@/api/common/model/chart'

defineOptions({ name: 'TreatedWater' })

const searchRef = ref()
const searchInfo = reactive({})
const typeList = ref<CommonTypeList[]>([])
const dictCode = 'water_science_wastewater_treatment_water'

onMounted(async () => {
  await getTypeList()
  searchRef.value.handleSearch()
})
const handleSearch = (params) => {
  Object.assign(searchInfo, params)
  getChartData()
}
const getTypeList = async () => {
  const res = await getDictListByCode(dictCode)
  typeList.value = res.map((item) => ({
    ...item,
    loading: true,
    chartData: {
      xaxisList: [],
      yaxisList: [],
    },
  }))
}
const splitName = (name) => {
  const index = name.indexOf('废水处理')
  return name.substring(0, index + 4);
}
const getChartData = () => {
  typeList.value.forEach((item) => {
    item.loading = false
    if (item.itemName == '分系统吨水单价') {
      item.type = 1,
        getChildDataCostDiv({
          ...searchInfo,
          dictCode,
          itemName: item.itemName,
        }).then((res) => {

          item.chartData = res

        })
        .finally(() => {
          item.loading = false
        })
    } else {
      item.type = 0,
        getSingleCaseUsageAndComprehensiveCost({
          ...searchInfo,
          dictCode,
          itemName: item.itemName,
        })
          .then((res) => {
            const { codeyAxisList, yaxisList, ...others } = res
            item.chartData = {
              codeyAxisList: codeyAxisList.map(item => ({
                ...item,
                name: splitName(item.name) + '成本'
              })),
              yaxisList: yaxisList.map(item => ({
                ...item,
                name: splitName(item.name) + '水量'
              })),
              ...others
            }
          })
          .finally(() => {
            item.loading = false
          })
    }

  })
  console.log(typeList.value);

}
</script>
<style scoped lang="scss">
.common-card {
  flex: 1;
  overflow: hidden;

  &.list-more {
    :deep(.ant-card-body) {
      overflow: auto !important;
    }

    .chart-box {
      height: 45% !important;
    }
  }

  :deep(.ant-card-body) {
    display: flex;
    flex: 1;
    flex-wrap: wrap;
    padding: 4px;
    overflow: hidden;

    .chart-box {
      flex-shrink: 0;
      width: 50%;
      height: 50%;
      min-height: 240px;
      padding: 4px;
    }
  }
}
</style>
